
using Microsoft.Services.Tools.BiztalkDocumenter;
using Microsoft.Services.Tools.BizTalkOM;

namespace Microsoft.Services.Tools.Publishers.CompiledHelp
{
    /// <summary>
    /// Summary description for HelpFileNode.
    /// </summary>
    public class HelpFileNode
    {
        private string _caption = string.Empty;
        private string _url = string.Empty;
        private readonly HelpFileNodeCollection _childNodes;

        public HelpFileNode()
        {
            _childNodes = new HelpFileNodeCollection();
        }

        private HelpFileNode(string caption)
            : this()
        {
            _caption = caption;
        }

        private HelpFileNode(string caption, string url)
            : this(caption)
        {
            _url = url;
        }

        public string Caption
        {
            get { return _caption; }
            set { _caption = value; }
        }

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        public HelpFileNodeCollection ChildNodes
        {
            get { return _childNodes; }
        }

        public HelpFileNode CreateChild(string caption)
        {
            return CreateChild(caption, string.Empty);
        }

        public HelpFileNode CreateChild(string caption, string url)
        {
            HelpFileNode hfn = new HelpFileNode(caption, url);
            _childNodes.Add(hfn);
            return hfn;
        }

        public void SortChildren()
        {
            CollectionSorter cs = new CollectionSorter(CollectionSorter.SortOrder.Ascending, "Caption");

            if (ChildNodes.Count > 0)
            {
                ChildNodes.Sort(cs);
            }
        }

        public override string ToString()
        {
            if (_url == string.Empty)
            {
                return string.Format("<LI><OBJECT type=\"text/sitemap\"><param name=\"Name\" value=\"{0}\"></OBJECT>", _caption);
            }
            
            return string.Format("<LI><OBJECT type=\"text/sitemap\"><param name=\"Name\" value=\"{0}\"><param name=\"Local\" value=\"{1}\"><param name=\"ImageNumber\" value=\"auto\"></OBJECT>",
                                 _caption,
                                 _url);
        }
    }
}
